const cloud = require('wx-server-sdk')

cloud.init()

const MONTH_OFFSET = 30 * 24 * 60 * 60 * 1000;

/// (skip: TimestampMillis = Date.now(), duration: TimestamMillis = MONTH_OFFSET, limit: int = 20) -> (result: DbQueryResult<Articles>)
exports.main = async (event) => {
  const skip = event.skip || Date.now();
  const duration = event.duration || MONTH_OFFSET;
  const limit = event.limit || 20;

  const db = cloud.database();
  const result = await db.collection('cards').where({
    publishTime: db.command.gt(skip - duration).lt(skip)
  }).limit(limit).get();

  return {
    result: result
  };
}
